package com.mmm.user.controller;

import com.mmm.common.vo.SuccessVo;
import com.mmm.user.entity.User;
import com.mmm.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 * @author One
 * @since 2022-03-03 15:25:54
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public SuccessVo<User> selectOne(@PathVariable Long id) {
        return new SuccessVo<>(this.userService.findById(id)) ;
    }
    @GetMapping("/ByToken")
    public SuccessVo<User> byToken(@RequestHeader("Authorization") String authorization) {
         return new SuccessVo<>(this.userService.findByToken(authorization)) ;
    }



    @PutMapping("/put")
    public SuccessVo<User> insert(@RequestBody User user) {
        return new SuccessVo<>(1, userService.insert(user));
    }

    @PostMapping("/update")
    public SuccessVo<User> update(@RequestBody User user) {
        return new SuccessVo<>(3,userService.update(user));
    }

    @DeleteMapping("/delete/{id}")
    public SuccessVo<Boolean> deleteById(@PathVariable Long id) {
        return new SuccessVo<>(2,userService.deleteById(id)) ;
    }

    @GetMapping("/list")
    public SuccessVo<com.mmm.common.vo.Paging<User>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size){
        return new SuccessVo<>( userService.list(page,size));
    }

    @GetMapping("/openId/{openId}")
    public SuccessVo<User> getByOpenId(@PathVariable String openId){
        return new SuccessVo<>(userService.findByOpenId(openId)) ;
    }
}